<?php

/**
 * This is the model class for table "{{jqcalendar}}".
 *
 * The followings are the available columns in table '{{jqcalendar}}':
 * @property integer $Id
 * @property string $Subject
 * @property string $Location
 * @property string $Description
 * @property string $StartTime
 * @property string $EndTime
 * @property integer $IsAllDayEvent
 * @property string $Color
 * @property string $RecurringRule
 * @property string $Stadium
 */
class Jqcalendar extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Jqcalendar the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{jqcalendar}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('IsAllDayEvent', 'required'),
			array('IsAllDayEvent', 'numerical', 'integerOnly'=>true),
			array('Subject', 'length', 'max'=>1000),
			array('Location, Color', 'length', 'max'=>200),
			array('Description', 'length', 'max'=>255),
			array('RecurringRule', 'length', 'max'=>500),
			array('Stadium', 'length', 'max'=>45),
			array('StartTime, EndTime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, Subject, Location, Description, StartTime, EndTime, IsAllDayEvent, Color, RecurringRule, Stadium', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => 'ID',
			'Subject' => 'Chủ đề',
			'Location' => 'Địa chỉ',
			'Description' => 'Mô tả',
			'StartTime' => 'Thời gian bắt đầu',
			'EndTime' => 'Thời gian kết thúc',
			'IsAllDayEvent' => 'Sự kiên cả ngày ?',
			'Color' => 'Màu',
			'RecurringRule' => 'Luật lặp',
			'Stadium' => 'Sân',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('Subject',$this->Subject,true);
		$criteria->compare('Location',$this->Location,true);
		$criteria->compare('Description',$this->Description,true);
		$criteria->compare('StartTime',$this->StartTime,true);
		$criteria->compare('EndTime',$this->EndTime,true);
		$criteria->compare('IsAllDayEvent',$this->IsAllDayEvent);
		$criteria->compare('Color',$this->Color,true);
		$criteria->compare('RecurringRule',$this->RecurringRule,true);
		$criteria->compare('Stadium',$this->Stadium,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function checkoutByStadium($stadium)
    {
        $model = self::model()->findByPk($stadium);
        
        $modelStadium = Stadiums::model()->with('type')->findByAttributes(array('name'=>$model->Stadium));
        
        // Liet ke tien gio
        
        $arrPrice = unserialize($modelStadium->all_price);
        $date = date('D',strtotime($model->StartTime));
        $hour = date('G',strtotime($model->StartTime));
        $price = 0;
        switch($date){
            case 'Mon':
                $price = $arrPrice['0'][$hour];
                break;
            case 'Tue':
                $price = $arrPrice['1'][$hour];
                break;
            case 'Wed':
                $price = $arrPrice['2'][$hour];
                break;
            case 'Thu':
                $price = $arrPrice['3'][$hour];
                break;
            case 'Fri':
                $price = $arrPrice['4'][$hour];
                break;
            case 'Sat':
                $price = $arrPrice['5'][$hour];
                break;
            case 'Sun':
                $price = $arrPrice['6'][$hour];
                break;                
        }

        $result = array();
        $result['id'] = $model->Id;
        $result['stadium'] = $model->Stadium . '<br/>Từ ' . date('h:i d-m-Y',strtotime($model->StartTime)) . '<br/> Đến ' . date('h:i d-m-Y',strtotime($model->EndTime)) ;
        if ($model)
            $result['time'] = (strtotime($model->EndTime) - strtotime($model->StartTime)) / 60;
        else
            $result['time'] = 0;
        $result['money'] = ($result['time'])/60 * $price;
        $result['total'] = $result['money'] - floatval($model->Bid);
        $result['bid'] = floatval($model->Bid);
        return $result;
    }
}